/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.wh.transaction.paymenttosupplier;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.ronsicecream.pos.entities.PosTransactionHoPaymentToSupplier;
import com.ronsicecream.pos.entities.PosTransactionHoReceiptFromSupplier;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class PaymentToSupplier extends Page {

    private Field paymentId;
    private Field paymentDate;
    private Field paymentNumber;
    private Field paymentTotal;
    private Field paymentKeel;
    private Field paymentCash;
    private Field paymentTimestamp;
    private Field paymentDescription;
    private Field paymentReceiptId;
    private Field paymentRegionalId;
    private Field paymentUnitId;
    private Field paymentUserId;

    public PaymentToSupplier() {
        super();

        setTheClass(PosTransactionHoPaymentToSupplier.class);

        paymentId = new Field();
        paymentId.setName("paymentId");
        paymentId.setCaption("id");
        //paymentId.setInfo("payment id");
        paymentId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        paymentId.setInputProperties(" size=\"20\"");
        getFields().add(paymentId);

        paymentDate = new Field();
        paymentDate.setName("paymentDate");
        paymentDate.setCaption("date");
        paymentDate.setRequired(true);
        paymentDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        paymentDate.setInputProperties(" size=\"8\"");
        paymentDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(paymentDate);

        paymentNumber = new Field();
        paymentNumber.setName("paymentNumber");
        paymentNumber.setCaption("number");
        paymentNumber.setRequired(true);
        paymentNumber.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        paymentNumber.setInputProperties(" size=\"30\" readonly");
        getFields().add(paymentNumber);

        paymentTotal = new Field();
        paymentTotal.setName("paymentTotal");
        paymentTotal.setCaption("total");
        //paymentTotal.setInfo("payment total");
        paymentTotal.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        paymentTotal.setInputProperties(" size=\"20\"");
        getFields().add(paymentTotal);

        paymentKeel = new Field();
        paymentKeel.setName("paymentKeel");
        paymentKeel.setCaption("keel");
        //paymentKeel.setInfo("payment keel");
        paymentKeel.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        paymentKeel.setInputProperties(" size=\"20\"");
        paymentKeel.getOptions().add(new String[]{"1", "YES"});
        paymentKeel.getOptions().add(new String[]{"2", "NO"});
        getFields().add(paymentKeel);

        paymentCash = new Field();
        paymentCash.setName("paymentCash");
        paymentCash.setCaption("cash");
        paymentCash.setRequired(true);
        paymentCash.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        paymentCash.setInputProperties(" size=\"20\"");
        getFields().add(paymentCash);

        paymentTimestamp = new Field();
        paymentTimestamp.setName("paymentTimestamp");
        paymentTimestamp.setCaption("timestamp");
        //paymentTimestamp.setInfo("payment timestamp");
        paymentTimestamp.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        paymentTimestamp.setInputProperties(" size=\"12\"");
//        paymentTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));
        getFields().add(paymentTimestamp);

        paymentDescription = new Field();
        paymentDescription.setName("paymentDescription");
        paymentDescription.setCaption("description");
        //paymentDescription.setInfo("payment description");
        paymentDescription.setListProperties(" align=\"left\" width=\"50%\" nowrap");
        paymentDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        paymentDescription.setInputType(Field.INPUT_TEXT_AREA);
        paymentDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(paymentDescription);

        paymentReceiptId = new Field();
        paymentReceiptId.setName("paymentReceiptId");
        paymentReceiptId.setCaption("receipt");
        paymentReceiptId.setRequired(true);
        paymentReceiptId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        paymentReceiptId.setInputProperties(" size=\"20\"");
        paymentReceiptId.setSelectorCondition("t.receiptKeel='1'");
        paymentReceiptId.getSelectorAction().add(new String[]{"value", "paymentTotal", "receiptUnpaid"});
        paymentReceiptId.getSelectorAction().add(new String[]{"value", "paymentCash", "receiptUnpaid"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptNumber", "Number", "width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptDate", " Date", "width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptTotal", "Total", " align=\"right\" width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptUnpaid", "Unpaid", " align=\"right\" width=\"10%\" nowrap"});
        paymentReceiptId.getSelectorColumns().add(new String[]{"receiptDescription", " Description", "width=\"70%\""});
        paymentReceiptId.setSelectorOrder("t.receiptNumber");
        getFields().add(paymentReceiptId);

        paymentRegionalId = new Field();
        paymentRegionalId.setName("paymentRegionalId");
        paymentRegionalId.setCaption("regional");
//        posMasterRegional.setInfo("pos master regional");   
        paymentRegionalId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        paymentRegionalId.setInputProperties(" size=\"20\"");
        getFields().add(paymentRegionalId);

        paymentUnitId = new Field();
        paymentUnitId.setName("paymentUnitId");
        paymentUnitId.setCaption("unit");
        //paymentUnitId.setInfo("pos master regional");
        paymentUnitId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        paymentUnitId.setInputProperties(" size=\"20\"");
        getFields().add(paymentUnitId);

        paymentUserId = new Field();
        paymentUserId.setName("paymentUserId");
        paymentUserId.setCaption("user");
        //paymentUserId.setInfo("pos master regional");
        paymentUserId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        paymentUserId.setInputProperties(" size=\"20\"");
        getFields().add(paymentUserId);

        fillFields();


        getFieldsAdd().clear();
        getFieldsAdd().add(paymentDate);
        getFieldsAdd().add(paymentReceiptId);
        getFieldsAdd().add(paymentTotal);
        getFieldsAdd().add(paymentCash);
        getFieldsAdd().add(paymentDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(paymentDate);
        getFieldsEdit().add(paymentReceiptId);
        getFieldsEdit().add(paymentTotal);
        getFieldsEdit().add(paymentCash);
        getFieldsEdit().add(paymentDescription);

        getFieldsList().clear();
        getFieldsList().add(paymentReceiptId);
        getFieldsList().add(paymentDate);
        getFieldsList().add(paymentTotal);
        getFieldsList().add(paymentCash);
        getFieldsList().add(paymentDescription);

        getFieldsView().clear();
        getFieldsView().add(paymentReceiptId);
        getFieldsView().add(paymentUserId);
        getFieldsView().add(paymentUnitId);
        getFieldsView().add(paymentTotal);
        getFieldsView().add(paymentCash);
        getFieldsView().add(paymentKeel);
        getFieldsView().add(paymentTimestamp);
        getFieldsView().add(paymentDescription);

        getActions().remove(Action.EDIT);
        getActions().remove(Action.DELETE);
        getActions().remove(Action.DELETE_MULTI);

        setController(new PaymentController(this));
        getController().setOrder(paymentTimestamp);
        getController().setOrderType(Controller.ORDER_DESC);

        setExportList(getClass().getResourceAsStream("PaymentList.jasper"));
        setExport(getClass().getResourceAsStream("Payment.jasper"));

    }

    @Override
    public String validate(Object data) {
        EntityManager em = getEntityManager();
        PosTransactionHoPaymentToSupplier payment = (PosTransactionHoPaymentToSupplier) data;
        //BigDecimal remaining = payment.getPaymentReceiptId().getReceiptTotal().subtract(payment.getPaymentReceiptId().getReceiptPaid());
        BigDecimal total = payment.getPaymentCash();
        if (total.compareTo(payment.getPaymentReceiptId().getReceiptTotal()) > 0) {
            MoneyFormatter formatter = MoneyFormatter.getInstance();
            return payment.getPaymentReceiptId() + " " + Dictionary.getValue(this, "Too much pay") + ", " + Dictionary.getValue(this, "total bill") + ": " + formatter.format(payment.getPaymentReceiptId().getReceiptTotal()) + " " + Dictionary.getValue(this, "money you pay now") + ": " + formatter.format(total);
        }
        return null;
    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        paymentDate.setFormater(DateFormatter.getInstanceDate(this));
        paymentTimestamp.setFormater(DateFormatter.getInstanceDateTime(this));
    }

    class PaymentController extends Controller {

        public PaymentController(Page page) {
            super(page);
        }

        @Override
        public void beforeInsert(Object data) {
            PosTransactionHoPaymentToSupplier payment = (PosTransactionHoPaymentToSupplier) data;
            payment.setPaymentUnitId(getCurrentUnit());
            payment.setPaymentUserId(getCurrentUser());
            payment.setPaymentTimestamp(Utility.getTimestamp());
            payment.setPaymentDate(Utility.getTimestamp());
//            payment.setPaymentPaid(payment.getPaymentReceiptId().getReceiptPaid().add(payment.getPaymentPay()));
            payment.getPaymentReceiptId();
            PosTransactionHoReceiptFromSupplier receipt = payment.getPaymentReceiptId();
            BigDecimal total = payment.getPaymentCash();
            BigDecimal subtot = receipt.getReceiptPaid().add(total);
            System.out.println("Subtotal :" + subtot);
            if (payment.getPaymentReceiptId().getReceiptTotal().compareTo(subtot) == 0) {
                payment.setPaymentKeel("2");

                payment.getPaymentReceiptId().setReceiptKeel("2");
                payment.getPaymentReceiptId().setReceiptPaid(subtot);
                System.out.println("keel : " + payment.getPaymentReceiptId().getReceiptKeel() + " paid :" + payment.getPaymentReceiptId().getReceiptPaid());
            } else {
                receipt.setReceiptPaid(subtot);
                payment.setPaymentKeel("1");
            }
        }
    }

    @Override
    public void loadDetail(List datas, String id) {
        EntityManager em = getEntityManager();
        List<PosTransactionHoPaymentToSupplier> request = em.createQuery("SELECT t FROM PosTransactionHoReceiptFromSupplier t WHERE t.receiptDetailId.receiptId=" + id.toString()).setHint(Persistence.REFRESH_HINT, true).getResultList();
        datas.clear();
        PosTransactionHoReceiptFromSupplier receipt = (PosTransactionHoReceiptFromSupplier) request;
        PosTransactionHoPaymentToSupplier payment = new PosTransactionHoPaymentToSupplier();
        payment.setPaymentTotal(receipt.getReceiptTotal());
        payment.setPaymentDescription(receipt.getReceiptDescription());
        datas.add(payment);
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Payment"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}
